package io.renren.modules.app.controller;

import io.renren.common.utils.R;
import io.renren.modules.app.req.weixin.WeChatUnifiedPayReq;
import io.renren.modules.app.service.WeChatPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author create by MR.XU email:260139486@qq.com
 * @projectName map-clothes-app-api
 * @Description: 微信支付
 * @date 2019-11-05
 */

@Controller
@RequestMapping("/api/v1/wxPay")
@Slf4j
@Api(tags = "APP微信支付")
public class WeChatPayController{

    @Resource
    private WeChatPayService weChatPayService;

    /**
      * @description: 微信支付前置--- 统一下单
      * @auther: create by MR.XU email:260139486@qq.com
      * @date 2019/11/5
      * @param weChatUnifiedPayReq
      * @return
      */
    @ApiOperation("统一支付下单")
    @PostMapping("/unifiedPaymentOrder")
    public R unifiedPaymentOrder(@RequestBody WeChatUnifiedPayReq weChatUnifiedPayReq, HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("=================[微信支付]统一下单请求参数:{}",weChatUnifiedPayReq.toString());
        Map<String, Object> respMap =  weChatPayService.unifiedPaymentOrder(weChatUnifiedPayReq,request,response);
        return R.ok();
    }

    /**
      * @description: 微信支付回调
      * @auther: create by MR.XU email:260139486@qq.com
      * @date 2019/11/6
      * @param req
      * @param resp
      * @return
      */

    @RequestMapping("/WxPayNotify")
    public void WxPayNotify(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        log.info("====================[微信支付]微信回调===================");
        weChatPayService.WxPayNotify(req,resp);
    }

    /**
      * @description: 微信支付结果查询
      * @auther: create by MR.XU email:260139486@qq.com
      * @date 2019/11/8
      * @param weChatUnifiedPayReq
      * @return
      */
    @ApiOperation("查询支付结果 必传payFlag, userId/merchantId , wxTransId ")
    @PostMapping("/wxPayQueryResult")
    public R wxPayQueryResult(@RequestBody WeChatUnifiedPayReq weChatUnifiedPayReq, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        log.info("====================[微信支付]支付结果查询参数:{}",weChatUnifiedPayReq.toString());
        Map<String,String> resultMap = weChatPayService.WxPayQueryResult(weChatUnifiedPayReq,req,resp);
        return R.ok();
    }

}
